package DataBase;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Vector;

import DataObjects.RecipeRightList;

public class UpdateCurrentRecipe  {
	private Vector<RecipeRightList> recipeItemsDetails;
	private Vector<String> recipeDetails;
	private int i=0;
	private int status=0;
	
	public UpdateCurrentRecipe(Vector<RecipeRightList> recipeItemsDetails,Vector<String> recipeDetails)
	{
		
		this.recipeItemsDetails=recipeItemsDetails;
		this.recipeDetails=recipeDetails;
	}
	


public void updateRecipe()
{
	SQLConnection sc=new SQLConnection();
	try {
				
		PreparedStatement ps2 = sc.getConnection().prepareStatement("UPDATE  recipe rec SET Rname=?,rmishkal=?,rconstPrice=?,rstatus=?  where Rid =?;");	
		sc.getConnection().setAutoCommit(false);
		System.out.println(recipeDetails);
		ps2.setString(1,(String) recipeDetails.get(1));
		ps2.setString(2,(String) recipeDetails.get(2));
		ps2.setString(3,(String) recipeDetails.get(3));
		ps2.setString(4,(String) recipeDetails.get(4));
		ps2.setString(5,(String) recipeDetails.get(0));
		ps2.executeUpdate();
		System.out.println("AAA");
		sc.getConnection().commit();
		ps2.close();
		PreparedStatement ps1 = sc.getConnection().prepareStatement("Delete FROM item_in_recipe Where  Rid =? ;");	
		//sc.getConnection().setAutoCommit(false);
		ps1.setString(1,(String) recipeDetails.get(0));
		ps1.executeUpdate();
		sc.getConnection().commit();
		while(i<recipeItemsDetails.size())
		{
					
			PreparedStatement ps = sc.getConnection().prepareStatement("Insert Into item_in_recipe (Rid,Iid,Iquantity) values(?,?,?);");
			ps.setString(1,(String) recipeDetails.get(0));
			ps.setInt(2, recipeItemsDetails.get(i).getItemID());
			ps.setFloat(3, recipeItemsDetails.get(i).getiQuantity());
			ps.executeUpdate();
			sc.getConnection().commit();
			
			i++;
		}
		ps1.close();
		ps2.close();
		sc.getConnection().close();
			//}
		status=1;			
	}
	
	catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	}





public int getStatus() {
	return status;
}





public void setStatus(int status) {
	this.status = status;
}
	

	

}
